package framework.common;

import java.util.Set;

import framework.sequential.Client;

public class RemoteSensor extends Sensor {

	private Client client;

	public RemoteSensor(Client client) {
		this.client = client;
	}

	@Override
	public SensorResponse sense(SensorRequest req) {
		// reqをDeviceRequestに隠ぺいする
		client.begin();
		client.addRequest(req);
		client.end();
		DeviceResponse res = client.request();
		return res.getResponses().get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Set<String> getAttributes() {
		SensorRequest req = new SensorRequest(SensorRequest.DIR, "", null);
		SensorResponse res = sense(req);
		return (Set<String>)res.getValue();
	}

	@Override
	public Object getAttribute(String attr) {
		SensorRequest req = new SensorRequest(SensorRequest.GET, attr, null);
		SensorResponse res = sense(req);
		return res.getValue();
	}

	@Override
	public void setAttribute(String attr, Object value) {
		SensorRequest req = new SensorRequest(SensorRequest.SET, attr, value);
		sense(req);
	}

}
